﻿using MiniExcelLibs;
using PanhuaWin.通用;
using Sunny.UI;
using Sunny.UI.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PanhuaWin.仓库.采购入库
{
    public partial class Form辅材入库 : UIPage
    {               /// <summary>
                    /// 临时表的guid
                    /// </summary>
        static string tguid = "";
        T_VIEW_CON t_view_con = new T_VIEW_CON();
        string 工厂编号1;
        string 工厂编号2;
        public Form辅材入库()
        {
            InitializeComponent();
        }

        private void Form辅材入库_Resize(object sender, EventArgs e)
        {
            this.uiTabControl1.Left = 0;
            this.uiTabControl1.Top = 0;
            this.uiTabControl1.Width = this.Width - 10;
            this.uiTabControl1.Height = this.Height - 10;
        }


        private void uiTextBox库位搜索1_TextChanged(object sender, EventArgs e)
        {
            //bai 仓库 库位不要联动
            //Do库位刷新1(this.uiComboBox仓库编号1.SelectedValue.ToString());
        }

        private void uiButton选择部门1_Click(object sender, EventArgs e)
        {
            Form部门档案 form部门档案 = new Form部门档案();
            form部门档案.工厂编号 = this.uiComboBoxWorkShop1.SelectedValue.ToString();
            if (form部门档案.ShowDialog(this) == DialogResult.OK)
            {
                this.uiTextBox部门1.Text = form部门档案.部门编号;
                this.uiTextBox部门名称1.Text = form部门档案.部门名称;
            }
        }

        private void uiButton合同查询_Click(object sender, EventArgs e)
        {
            List辅料采购合同();
        }

        private void List辅料采购合同()
        {
            string sql;
            //人员权限问题
            //
            sql = @"select ROW_NUMBER() OVER(  ORDER BY guid) 行号,  guid, CID, T_APP_DPT_CID, T_APP_DPT_NAME, T_APP_USER_CODE, T_APP_USER_NAME, 
                T_APP_USER_TEL, T_APP_USER_DATE, T_EXECUTOR_NAME, T_EXECUTOR_CODE, EXP40, T_DRAFT_NAME, 
                T_RELATED_NAME, T_RELATED_CID, T_CONTRACT_CODE, T_CONTRACT_NAME, T_CONTRACT_TYPE_NAME, 
                T_CONTRACT_TYPE_CID, T_CONTRACT_MONEY, T_MONEY_UNIT, T_MONEY_CAPITAILZATION, T_RATE, 
                T_RATE_BEFOR_MONEY, T_TEAN_TYPE, T_START_DATE, T_END_DATE, T_TEAN, T_TEAN_UNIT, T_PAY_WAY, 
                T_PAY_WAY_CID, T_PROJECT_NAME, T_PROJECT_CID, T_PROJECT_USER_NAME, T_PROJECT_USER_CODE, 
                T_REMARK, T_SUBJECT_NAME, T_SUBJECT_CID, T_SEAL, T_SEAL_PATH, T_GOOD_CID, T_ORDER, T_GOOD_CODE, 
                T_GOOD_NAME, T_GOOD_MODEL, T_GOOD_PRICE, T_GOOD_NUM, T_GOOD_UNIT, T_GOOD_MONEY, T_GOOD_TIME, 
                T_GOOD_ADDRESS, T_GOOD_REMARK, T_GOOD_TRANSACTION_NUM, T_GOOD_EXP1, T_GOOD_EXP2, 
                T_GOOD_EXP3, T_GOOD_EXP4, T_RELATIVE_NAME
FROM      T_VIEW_CON ";
            sql += " where T_APP_USER_DATE >='" + this.uiDatePicker开始日期.Value + "' and T_APP_USER_DATE<'" + this.uiDatePicker结束日期.Value.AddDays(-1) + "'";
            sql += " and T_SUBJECT_NAME='" + this.uiComboBoxWorkShop1.Text + "'";
            sql += " and T_CONTRACT_TYPE_NAME='" + this.uiComboBox合同类型.Text + "'";
            sql += " order by T_CONTRACT_CODE,T_ORDER";
            if (uiTextBoxGood.Text != "")
            {
                sql += " and T_GOOD_NAME like '%" + this.uiTextBoxGood.Text.Trim().Replace("--", "") + "%'";

            }
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            this.uiDataGridView采购合同.AutoGenerateColumns = false;
            this.uiDataGridView采购合同.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            this.uiDataGridView采购合同.DataSource = tb;
            this.uiDataGridView采购合同.Refresh();
        }

        private void uiButton选择存货1_Click(object sender, EventArgs e)
        {
            Form存货档案 form存货档案 = new Form存货档案();
            form存货档案.存货分类 = "生产辅料";
            if (form存货档案.ShowDialog(this) == DialogResult.OK)
            {
                this.uiTextBox存货编号1.Text = form存货档案.存货档案.T_GOOD_CODE;
                this.uiTextBox存货名称1.Text = form存货档案.存货档案.T_GOOD_NAME;
                this.uiTextBox规格型号1.Text = form存货档案.存货档案.T_GOOD_MODEL;
                this.uiTextBox计量单位1.Text = form存货档案.存货档案.T_GOOD_UNIT;

            }
        }

        private void uiButton添加1_Click(object sender, EventArgs e)
        {
            if (tguid == "")
            {
                tguid = Guid.NewGuid().ToString();
            }
            string sql;
            int 序号;

            string 合同号 = this.uiTextBox选中的合同号1.Text.Trim().Replace("--", "");
            string 合同行号 = this.uiTextBox选中的合同行1.Text.Trim().Replace("--", "");
            string 存货编号 = this.uiTextBox存货编号1.Text.Trim().Replace("--", "");
            string 存货名称 = this.uiTextBox存货名称1.Text.Trim().Replace("--", "");
            string 规格型号 = this.uiTextBox规格型号1.Text.Trim().Replace("--", "");
            string 计量单位 = this.uiTextBox计量单位1.Text.Trim().Replace("--", "");
            string 批次号 = this.uiTextBox批次号1.Text.Trim().Replace("--", "");
            decimal 入库数量 = pub.Todecimal(this.uiTextBox入库数量1.Text.Trim().Replace("--", ""));
           
            #region 检查
            if (this.uiComboBox仓库编号1.SelectedValue == null || this.uiComboBox仓库编号1.SelectedValue.ToString() == "")
            {
                MessageBox.Show("入库仓库有问题!", "提示", MessageBoxButtons.OK);
                return;
            }
            if (this.uiComboBox入库库位1.SelectedValue == null || this.uiComboBox入库库位1.SelectedValue.ToString() == "")
            {
                MessageBox.Show("入库库位有问题!", "提示", MessageBoxButtons.OK);
                return;
            }

            string 入库仓库 = this.uiComboBox仓库编号1.SelectedValue.ToString();
            string 入库库位 = this.uiComboBox入库库位1.SelectedValue.ToString();

            if (合同号 == "")
            {
                MessageBox.Show("合同号未选择,请重新选择!", "提示", MessageBoxButtons.OK);
                return;
            }
            if (合同行号 == "")
            {
                MessageBox.Show("合同行未选择,请重新选择!", "提示", MessageBoxButtons.OK);
                return;
            }
            if (存货编号 == "")
            {
                MessageBox.Show("存货编号未选择,请重新选择!", "提示", MessageBoxButtons.OK);
                return;
            }
            if (入库仓库 == "")
            {
                MessageBox.Show("入库仓库有问题!", "提示", MessageBoxButtons.OK);
                return;
            }
            if (入库库位 == "")
            {
                MessageBox.Show("入库库位有问题!", "提示", MessageBoxButtons.OK);
                return;
            }
            ///检查原料卷号是否存在了
            //bai 锌锭批次号不能重复，其它批次号都可重复
            if (存货名称 == "锌锭")
            {
                if (pub.IsExistBatch(存货编号, 批次号))
                {
                    MessageBox.Show("批次号已经存在不能添加了!", "提示", MessageBoxButtons.OK);
                    return;

                }
                if (pub.IsExistTempBatch(存货编号, 批次号))
                {
                    MessageBox.Show("批次号已经存在不能添加了!", "提示", MessageBoxButtons.OK);
                    return;

                }
            }
            
            if (入库数量 == 0)
            {
                MessageBox.Show("数量不能为0!", "提示", MessageBoxButtons.OK);
                return;
            }
            序号 = this.uiDataGridView辅料1.Rows.Count + 1;
            if (t_view_con.T_CONTRACT_CODE == "")
            {
                MessageBox.Show("合同号未选择,请重新选择!", "提示", MessageBoxButtons.OK);
                return;
            }
            #endregion
            ///塞入临时表并显示
            string T_GOOD_NUM = "0";
            string T_GOOD_PRICE = "0";
            string T_GOOD_MONEY = "0";

            if (!string.IsNullOrEmpty(t_view_con.T_GOOD_NUM))
            {
                T_GOOD_NUM = t_view_con.T_GOOD_NUM;
            }
            if (!string.IsNullOrEmpty(t_view_con.T_GOOD_PRICE))
            {
                T_GOOD_PRICE = t_view_con.T_GOOD_PRICE;
            }
            if (!string.IsNullOrEmpty(t_view_con.T_GOOD_MONEY))
            {
                T_GOOD_MONEY = t_view_con.T_GOOD_MONEY;
            }
            sql = @"insert into Temp_辅料入库 (guid, 序号, 合同号, 合同行号, 入库仓库,入库库位,存货编号, 存货名称, 规格型号, 批次号, 入库数量 ,计量单位, MAC, 时间, 
                T_GOOD_NUM, T_GOOD_PRICE, T_GOOD_MONEY, CID )";
            sql += " values ('" + tguid + "', '" + 序号 + "', '" + 合同号 + "', '" + 合同行号 + "','" + 入库仓库 + "','" + 入库库位 + "', '" + 存货编号 + "', '" + 存货名称 + "', '" + 规格型号 + "',  '" + 批次号 + "','" + 入库数量 + "','" + 计量单位 + "',";
            sql += "'" + pub.getMac() + "',getdate(),'" +  T_GOOD_NUM + "','" + T_GOOD_PRICE + "','" + T_GOOD_MONEY + "','" + t_view_con.CID + "' )";
            if (pub.ExecuteSql(pub.ConnectionString, sql, "塞入辅料入库临时表") > 0)
            {
                //更新序号
                sql = "select guid, 序号   FROM      Temp_辅料入库 where guid='" + tguid + "'  order by 序号 ";
                DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
                if (tb != null)
                {
                    for (int i = 0; i < tb.Rows.Count; i++)
                    {
                        sql = "update Temp_辅料入库 set 序号='" + (i + 1) + "'   where guid='" + tguid + "' and 序号='" + tb.Rows[i]["序号"].ToString() + "'";
                        pub.ExecuteSql(pub.ConnectionString, sql, "更新辅料入库临时表");
                    }
                }
                ListTemp_辅料入库(tguid, this.uiDataGridView辅料1);
            }
            else
            {
                MessageBox.Show("添加失败了请检查网络或者其他原因!", "提示", MessageBoxButtons.OK);
                return;

            }
        }

        private void ListTemp_辅料入库(string guid, UIDataGridView uidatagridview)
        {
            string sql;
            sql = "select  guid, 序号, 合同号, 合同行号, 入库仓库,入库库位,存货编号, 存货名称, 规格型号, 批次号, 入库数量 ,计量单位 ";
            sql += " FROM Temp_辅料入库  where guid='" + guid + "' order by 序号 ";
            DataTable tb1 = pub.GetDataTable(pub.ConnectionString, sql);
            uidatagridview.AutoGenerateColumns = false;
            uidatagridview.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            uidatagridview.DataSource = tb1;
            uidatagridview.Refresh();
        }

        private void uiButton导出模板1_Click(object sender, EventArgs e)
        {
            string path;
            this.saveFileDialog1.Filter = "Excel 文件(*.xlsx)|*.xlsx ";
            string sql;
            sql = " select   序号, 合同号, 合同行号, 入库仓库,入库库位,存货编号, 存货名称, 规格型号, 批次号, 入库数量 ,计量单位 ";
            sql += " FROM Temp_辅料入库 where  1=2";
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            if (this.saveFileDialog1.ShowDialog(this) == DialogResult.OK)
            {
                path = this.saveFileDialog1.FileName;
                if (!File.Exists(path))
                {
                    MiniExcel.SaveAs(path, tb, true, "sheet1", ExcelType.XLSX);
                }
                else
                {
                    MessageBox.Show("文件已经存在,请重试!", "提示", MessageBoxButtons.OK);
                    return;
                }

            }
        }

        private void uiButton导入1_Click(object sender, EventArgs e)
        {
            try
            {
                string path;
                this.openFileDialog1.Filter = "Excel 文件(*.xlsx)|*.xlsx ";
                if (this.openFileDialog1.ShowDialog(this) == DialogResult.OK)
                {
                    path = this.openFileDialog1.FileName;
                    DataTable table = (DataTable)MiniExcel.QueryAsDataTable(path, useHeaderRow: true);
                    if (table.Rows.Count > 0)
                    {
                        if (tguid == "")
                        {
                            tguid = Guid.NewGuid().ToString();
                        }
                        for (int i = 0; i < table.Rows.Count; i++)
                        {

                            string sql;
                            int 序号;
                            string 合同号;
                            if (table.Rows[i]["合同行号"].ToString() != "")
                            {
                                合同号 = table.Rows[i]["合同号"].ToString();
                            }
                            else
                            {
                                合同号 = this.uiTextBox选中的合同号1.Text;
                            }
                            string 合同行号;
                            if (table.Rows[i]["合同行号"].ToString() != "")
                            {
                                合同行号 = table.Rows[i]["合同行号"].ToString();
                            }
                            else
                            {
                                合同行号 = this.uiTextBox选中的合同行1.Text;
                            }

                            string 存货编号 = table.Rows[i]["存货编号"].ToString();
                            string 存货名称 = table.Rows[i]["存货名称"].ToString();
                            string 规格型号 = table.Rows[i]["规格型号"].ToString();
                            string NC存货编号 = pub.GetInventoryCode(存货名称, 规格型号);
                            if (NC存货编号 == "")
                            {
                                MessageBox.Show("第" + i.ToString() + "行【" + 存货名称 + "," + 规格型号 + "】的信息有问题,请确认数据!", "提示", MessageBoxButtons.OK);
                                pub.Delete辅料入库临时数据();
                                return;
                            }
                            else
                            {
                                if (存货编号 == "")
                                {
                                    存货编号 = NC存货编号;
                                }
                                else
                                {
                                    if (!pub.IsExistInventory(存货编号))
                                    {
                                        MessageBox.Show("第" + i.ToString() + "行【" + 存货编号 + "】的信息不存在,请确认数据!", "提示", MessageBoxButtons.OK);
                                        pub.Delete辅料入库临时数据();
                                        return;
                                    }
                                    if (存货编号 != NC存货编号)
                                    {
                                        MessageBox.Show("第" + i.ToString() + "行【" + 存货编号 + "】和【" + 存货名称 + "," + 规格型号 + "】的信息有冲突,请确认数据!", "提示", MessageBoxButtons.OK);
                                        pub.Delete辅料入库临时数据();
                                        return;
                                    }

                                }
                            }


                            string 计量单位 = table.Rows[i]["计量单位"].ToString();
                            string 批次号 = table.Rows[i]["批次号"].ToString();
                            decimal 入库数量 = pub.Todecimal(table.Rows[i]["入库数量"].ToString());
                            string 入库仓库 = pub.getWareHouse(table.Rows[i]["入库仓库"].ToString());
                            string 入库库位 = pub.getLocation(table.Rows[i]["入库库位"].ToString());
                            #region 检查
                            if (合同号 == "")
                            {
                                MessageBox.Show("合同号未选择,请重新选择!", "提示", MessageBoxButtons.OK);
                                pub.Delete辅料入库临时数据();
                                return;
                            }
                            if (合同行号 == "")
                            {
                                MessageBox.Show("合同行未选择,请重新选择!", "提示", MessageBoxButtons.OK);
                                pub.Delete辅料入库临时数据();
                                return;
                            }
                            if (存货编号 == "")
                            {
                                MessageBox.Show("存货编号未选择,请重新选择!", "提示", MessageBoxButtons.OK);
                                pub.Delete辅料入库临时数据();
                                return;
                            }
                            if (批次号 == "")
                            {
                                MessageBox.Show("批次号未填写!", "提示", MessageBoxButtons.OK);
                                pub.Delete辅料入库临时数据();
                                return;
                            }
                            if (入库数量 <= 0)
                            {
                                MessageBox.Show("入库数量有问题!", "提示", MessageBoxButtons.OK);
                                pub.Delete辅料入库临时数据();
                                return;
                            }
                            if (入库仓库 == "")
                            {
                                MessageBox.Show("入库仓库有问题!", "提示", MessageBoxButtons.OK);
                                pub.Delete辅料入库临时数据();
                                return;
                            }
                            if (入库库位 == "")
                            {
                                MessageBox.Show("入库库位有问题!", "提示", MessageBoxButtons.OK);
                                pub.Delete辅料入库临时数据();
                                return;
                            }
                            ///检查辅料存货编号和批次是否存在了
                            if (pub.IsExistBatch(存货编号, 批次号))
                            {
                                MessageBox.Show("存货编号,批次号已经存在了,不能再次入库!", "提示", MessageBoxButtons.OK);
                                pub.Delete辅料入库临时数据();
                                return;

                            }
                            if (IsExistTempBatch(存货编号, 批次号))
                            {
                                MessageBox.Show("存货编号,批次号这次已经有添加了,不能再次添加!", "提示", MessageBoxButtons.OK);
                                pub.Delete辅料入库临时数据();
                                return;
                            }

                            序号 = i + 1;
                            t_view_con = pub.GetT_VIEW_CON(合同号, 合同行号);
                            if (t_view_con == null)
                            {
                                MessageBox.Show("合同号未选择,请重新选择!", "提示", MessageBoxButtons.OK);
                                pub.Delete辅料入库临时数据();
                                return;
                            }
                            #endregion

                            ///塞入临时表并显示
                            sql = @"insert into Temp_辅料入库 (guid, 序号, 合同号, 合同行号, 入库仓库,入库库位,存货编号, 存货名称, 规格型号, 批次号, 入库数量 ,计量单位, MAC, 时间,T_GOOD_NUM, T_GOOD_PRICE, T_GOOD_MONEY, CID )";
                            sql += " values ('" + tguid + "', '" + 序号 + "', '" + 合同号 + "', '" + 合同行号 + "','" + 入库仓库 + "','" + 入库库位 + "', '" + 存货编号 + "', '" + 存货名称 + "', '" + 规格型号 + "',  '" + 批次号 + "','" + 计量单位 + "',";
                            sql += "'" + pub.getMac() + "',getdate(),'" + t_view_con.T_GOOD_NUM + "','" + t_view_con.T_GOOD_PRICE + "','" + t_view_con.T_GOOD_MONEY + "','" + t_view_con.CID + "' )";
                            if (pub.ExecuteSql(pub.ConnectionString, sql, "塞入辅料入库临时表") > 0)
                            {


                            }
                            else
                            {
                                MessageBox.Show("添加失败了请检查网络或者其他原因!", "提示", MessageBoxButtons.OK);

                                return;

                            }
                        }
                        ListTemp_辅料入库(tguid, this.uiDataGridView辅料1);
                    }


                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("导入失败了!" + ex.ToString(), "提示", MessageBoxButtons.OK);
                pub.Delete辅料入库临时数据();
                return;
            }
        }



        private bool IsExistTempBatch(string 存货编号, string 批次号)
        {
            string sql;
            sql = "select * from Temp_辅料入库 where  存货编号='" + 存货编号 + "' and 批次号='" + 批次号 + "'";
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            if (tb.Rows.Count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private void uiButton新建入库单1_Click(object sender, EventArgs e)
        {
            ///清空界面所选
            if (tguid == "")
            {
                tguid = Guid.NewGuid().ToString();
                ClearAll1();

            }
            else
            {
                DeleteTemp(tguid);
                tguid = Guid.NewGuid().ToString();
                ClearAll1();
            }
        }

        private void DeleteTemp(string tguid)
        {
            string sql;
            sql = "delete Temp_辅料入库 where guid ='" + tguid + "' or mac='" + pub.getMac() + "'";
            pub.ExecuteSql(pub.ConnectionString, sql, "删除原料入库的临时表");
        }

        private void ClearAll1()
        {
            this.uiTextBox批次号1.Text = "";
            this.uiTextBox入库数量1.Text = "";
            this.uiTextBox存货编号1.Text = "";
            this.uiTextBox存货名称1.Text = "";
            this.uiTextBox规格型号1.Text = "";
            this.uiTextBox计量单位1.Text = "";
            this.uiTextBox选中的合同号1.Text = "";
            this.uiTextBox选中的合同行1.Text = "";

            this.uiDataGridView辅料1.DataSource = null;
            this.uiDataGridView辅料1.Refresh();

        }

        private void uiButton保存1_Click(object sender, EventArgs e)
        {
            ///将临时表塞入入库单
            ///
            string sql;
            bool t = false;
            ///先验证是否重复了
            sql = "select guid, 序号, 合同号, 合同行号, 存货编号, 存货名称, 规格型号, 计量单位,入库数量, 批次号    FROM Temp_辅料入库 where guid='" + tguid + "'";
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            if (tb.Rows.Count > 0)
            {
                for (int i = 0; i < tb.Rows.Count; i++)
                {
                    if (pub.IsExistBatch(tb.Rows[i]["存货编号"].ToString(), tb.Rows[i]["批次号"].ToString()))
                    {

                        MessageBox.Show("要保存辅料【" + tb.Rows[i]["存货编号"].ToString() + "," + tb.Rows[i]["批次号"].ToString() + "】已经有保存过了!", "提示", MessageBoxButtons.OK);
                        return;
                    }
                }
            }
            string stockInType = "辅料入库";

            string workShopCode = 工厂编号1;
            string docCode = pub.GetDocCode("辅料入库", workShopCode);
            if (this.uiTextBox部门1.Text == "")
            {
                MessageBox.Show("部门未选择!", "提示", MessageBoxButtons.OK);
                return;
            }
            string departCode = pub.GetDepartByCode(this.uiTextBox部门1.Text, 工厂编号1).DepartCode;
            string guid = Guid.NewGuid().ToString();
            ///塞入原料主表
            if (docCode == "")
            {
                MessageBox.Show("未获得单号,请联系系统管理员!", "提示", MessageBoxButtons.OK);
                return;
            }
            //抛NC
            sendToNC();

            sql = " insert into Bus_StockInM(guid, StockInType, WorkShopCode, DocCode,  DepartCode, CreateID, Creator, CreateDate,IsSendToNC)";
            sql += " values('" + guid + "','" + stockInType + "','" + workShopCode + "','" + docCode + "', '" + departCode + "','" + pub.sys_user.User_Id + "','" + pub.sys_user.UserTrueName + "', getdate(),'N')";
            sql += @" insert into Bus_StockInMaterial(guid, Bguid, DocCode, RowNumber, WorkShopCode, WarehouseCode, StorageLocation, T_CONTRACT_CODE, 
                T_ORDER, T_GOOD_UNIT, T_GOOD_NUM, T_GOOD_PRICE, T_GOOD_MONEY, InventoryCode,QuantityMain,QuantityAssist, Size, CID, Batch
 )";
            sql += " select  '" + guid + "',newid(),'" + docCode + "',  序号,'" + workShopCode + "', 入库仓库,入库库位,合同号,合同行号,计量单位,isnull(T_GOOD_NUM,0), ";
            sql += " isnull(T_GOOD_PRICE,0), isnull(T_GOOD_MONEY,0),存货编号,入库数量,1,规格型号,CID ,批次号";
            sql += " from  Temp_辅料入库 where guid='" + tguid + "'";
         int r =  pub.ExecuteSql(pub.ConnectionString, sql, "保存原料入库单据");
            ///检查是否成功
            sql = "select * from Bus_StockInM where guid='" + guid + "' ";
            DataTable tbr = pub.GetDataTable(pub.ConnectionString, sql);
            if (tbr.Rows.Count > 0)
            {
                sql = "select * from Bus_StockInMaterial where guid='" + guid + "'";
                DataTable tbb = pub.GetDataTable(pub.ConnectionString, sql);
                if (tbb.Rows.Count > 0)
                {
                    MessageBox.Show("保存成功单号【" + docCode + "】!", "提示", MessageBoxButtons.OK);
                    sql = "delete  Temp_辅料入库 where guid='" + tguid + "' or mac ='" + pub.getMac() + "'";
                    pub.ExecuteSql(pub.ConnectionString, sql, "保存成功后删除原料入库临时表");
                    tguid = "";
                    ClearAll1();

                    return;
                }
                else
                {
                    sql = "delete  Bus_StockInM where guid='" + guid + "'";

                    pub.ExecuteSql(pub.ConnectionString, sql, "因为保存失败了，删除辅料入库单");
                    MessageBox.Show("保存失败,代码【err1】!", "提示", MessageBoxButtons.OK);
                    return;
                }
            }
            else
            {
                sql = "delete  Bus_StockInMaterial  where guid='" + guid + "'";

                pub.ExecuteSql(pub.ConnectionString, sql, "因为保存失败了，删除辅料入库单");
                MessageBox.Show("保存失败,代码【err2】!", "提示", MessageBoxButtons.OK);
                return;
            }
        }

        //保存时候抛NC
        private void sendToNC() {

        }

        private void uiDataGridView辅料1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                if (this.uiDataGridView辅料1.Columns[e.ColumnIndex].Name == "D删除")
                {
                    string sql;
                    int 序号 = pub.ToInt(this.uiDataGridView辅料1.Rows[e.RowIndex].Cells["D序号"].Value.ToString());
                    sql = " delete Temp_辅料入库 where (guid='" + tguid + "' and 序号='" + 序号 + "') or (mac ='" + pub.getMac() + "' and guid<>'" + tguid + "')";
                    if (pub.ExecuteSql(pub.ConnectionString, sql, "辅料入库临时表删除行") >= 0)
                    {
                        //更新序号
                        sql = "select guid, 序号   FROM      Temp_辅料入库 where guid='" + tguid + "'  order by 序号 ";
                        DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
                        if (tb != null)
                        {
                            for (int i = 0; i < tb.Rows.Count; i++)
                            {
                                sql = "update Temp_辅料入库 set 序号='" + (i + 1) + "'   where guid='" + tguid + "' and 序号='" + tb.Rows[i]["序号"].ToString() + "'";
                                pub.ExecuteSql(pub.ConnectionString, sql, "更新辅料入库临时表的行号");
                            }
                        }
                        ListTemp_辅料入库(tguid, this.uiDataGridView辅料1);
                    }
                    else
                    {
                        MessageBox.Show("删除出错了,请检查");
                        return;
                    }

                }
            }
        }

        private void uiLabel29_Click(object sender, EventArgs e)
        {

        }

        private void Form辅材入库_Load(object sender, EventArgs e)
        {
            this.uiDatePicker开始日期.Value = DateTime.Today.AddMonths(-1);
            this.uiDatePicker结束日期.Value = DateTime.Today;
            pub.ListWorkShop(pub.sys_user, this.uiComboBoxWorkShop1);
            pub.ListWorkShop(pub.sys_user, this.uiComboBoxWorkShop2);
            pub.ListContractType(this.uiComboBox合同类型);
            pub.ListAppUserName(this.uiComboBox申请人);
            pub.ListGoods(this.uiComboBoxGoods);
            Do库位刷新1();

        }

        private void uiComboBoxWorkShop1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string sql;
            工厂编号1 = this.uiComboBoxWorkShop1.SelectedValue.ToString();
            sql = @"SELECT   guid, WorkShopCode, WorkShopName, AuditDate, AuditStatus, AuditId, Auditor, Remark, CreateID, Creator, 
                CreateDate, ModifyID, Modifier, ModifyDate   FROM      Dic_WorkShop     where  WorkShopCode='" + 工厂编号1 + "'      ";
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            if (tb.Rows.Count > 0)
            {
                //                sql = @"SELECT    guid, WorkShopGuid, WarehouseCode, WarehouseName, WarehouseDesc, AuditDate, AuditStatus, AuditId, 
                //                Auditor, Remark, CreateID, Creator, CreateDate, ModifyID, Modifier, ModifyDate
                //FROM      Dic_Warehouse where WorkShopGuid ='" + tb.Rows[0]["guid"].ToString() + "'";
                //                DataTable tb2 = pub.GetDataTable(pub.ConnectionString, sql);
                pub.ListWareHouse(this.uiComboBox仓库编号1, tb.Rows[0]["guid"].ToString());
                if (this.uiComboBox仓库编号1.SelectedValue != null && this.uiComboBox仓库编号1.SelectedValue.ToString() != "")
                {
                   // Do库位刷新1(this.uiComboBox仓库编号1.SelectedValue.ToString());
                }

            }
        }

        private void Do库位刷新1(string 仓库编号)
        {
            string sql;

            sql = @"SELECT   guid, WorkShopCode, WorkShopName, AuditDate, AuditStatus, AuditId, Auditor, Remark, CreateID, Creator, 
                CreateDate, ModifyID, Modifier, ModifyDate   FROM      Dic_WorkShop     where  WorkShopCode='" + 工厂编号1 + "'      ";
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            if (tb.Rows.Count > 0)
            {
                this.uiComboBox入库库位1.DataSource = null;
                uiComboBox入库库位1.Refresh();
                pub.ListLocation(this.uiComboBox入库库位1, 工厂编号1, this.uiTextBox库位搜索1.Text, 仓库编号);
            }
        }
        //bai 仓库不联动
        private void Do库位刷新1()
        {
            string sql;
            sql = @"SELECT   dbo.Dic_Location.LocationCode, dbo.Dic_WorkShop.WorkShopCode, dbo.Dic_Warehouse.WarehouseCode
                    FROM      dbo.Dic_Location INNER JOIN
                    dbo.Dic_Warehouse ON dbo.Dic_Location.WarehouseGuid = dbo.Dic_Warehouse.guid INNER JOIN
                    dbo.Dic_WorkShop ON dbo.Dic_Warehouse.WorkShopGuid = dbo.Dic_WorkShop.guid  ";
           
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            if (tb != null)
            {
                uiComboBox入库库位1.DataSource = tb;
                uiComboBox入库库位1.ValueMember = "LocationCode";
                uiComboBox入库库位1.DisplayMember = "LocationCode";
                uiComboBox入库库位1.Refresh();
            }
            else
            {
                uiComboBox入库库位1.DataSource = null;
                uiComboBox入库库位1.Refresh();
            }
        }

        private void uiDataGridView采购合同_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                string cid = this.uiDataGridView采购合同.Rows[e.RowIndex].Cells["C合同CID"].Value.ToString();
                t_view_con = pub.GetT_VIEW_CON(cid);
                if (t_view_con != null)
                {
                    this.uiTextBox存货编号1.Text = t_view_con.T_GOOD_CODE.ToString();
                    this.uiTextBox选中的合同号1.Text = t_view_con.T_CONTRACT_CODE.ToString();
                    this.uiTextBox选中的合同行1.Text = t_view_con.T_ORDER.ToString();
                    this.uiTextBox存货名称1.Text = t_view_con.T_GOOD_NAME.ToString();
                    this.uiTextBox规格型号1.Text = t_view_con.T_GOOD_MODEL.ToString();
                    this.uiTextBox计量单位1.Text = t_view_con.T_GOOD_UNIT.ToString();
                    //if (this.uiTextBox存货名称1.Text == "锌锭")
                    //{
                    //    uiTextBox计量单位1.Text = "Kg";
                    //}
                }
                else
                {
                    this.uiTextBox存货编号1.Text = "";
                    this.uiTextBox选中的合同号1.Text = "";
                    this.uiTextBox选中的合同行1.Text = "";
                    this.uiTextBox存货名称1.Text = "";
                    this.uiTextBox规格型号1.Text = "";
                    this.uiTextBox计量单位1.Text = "";

                }
            }
        }

        private void uiButton选择部门2_Click(object sender, EventArgs e)
        {
            Form部门档案 form部门档案 = new Form部门档案();
            form部门档案.工厂编号 = this.uiComboBoxWorkShop2.SelectedValue.ToString();
            if (form部门档案.ShowDialog(this) == DialogResult.OK)
            {
                this.uiTextBox部门2.Text = form部门档案.部门编号;
                this.uiTextBox部门名称2.Text = form部门档案.部门名称;
            }
        }

        private void uiTextBox库位搜索2_TextChanged(object sender, EventArgs e)
        {
            Do库位刷新2(this.uiComboBox仓库编号2.SelectedValue.ToString());
        }

        private void Do库位刷新2(string 仓库编号)
        {
            string sql;

            sql = @"SELECT   guid, WorkShopCode, WorkShopName, AuditDate, AuditStatus, AuditId, Auditor, Remark, CreateID, Creator, 
                CreateDate, ModifyID, Modifier, ModifyDate   FROM      Dic_WorkShop     where  WorkShopCode='" + 工厂编号2 + "'      ";
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            if (tb.Rows.Count > 0)
            {
                this.uiComboBox入库库位2.DataSource = null;
                uiComboBox入库库位2.Refresh();
                pub.ListLocation(this.uiComboBox入库库位2, 工厂编号2, this.uiTextBox库位搜索2.Text, 仓库编号);
            }
        }

        private void uiButton选择存货2_Click(object sender, EventArgs e)
        {
            Form存货档案 form存货档案 = new Form存货档案();

            form存货档案.存货分类 = "生产辅料";

            if (form存货档案.ShowDialog(this) == DialogResult.OK)
            {
                this.uiTextBox存货编号2.Text = form存货档案.存货档案.T_GOOD_CODE;
                this.uiTextBox存货名称2.Text = form存货档案.存货档案.T_GOOD_NAME;
                this.uiTextBox规格型号2.Text = form存货档案.存货档案.T_GOOD_MODEL;
                this.uiTextBox计量单位2.Text = form存货档案.存货档案.T_GOOD_UNIT;

            }
        }

        private void uiTextBox供应商条件2_TextChanged(object sender, EventArgs e)
        {
            pub.ListSupplier(this.uiComboBox供应商2, this.uiTextBox供应商条件2.Text);
        }

        private void uiTabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (uiTabControl1.SelectedIndex == 1)
            {
                pub.ListSupplier(this.uiComboBox供应商2, this.uiTextBox供应商条件2.Text);
            }
        }

        private void uiButton导出模板2_Click(object sender, EventArgs e)
        {
            string path;
            this.saveFileDialog1.Filter = "Excel 文件(*.xlsx)|*.xlsx";
            string sql;
            sql = " select   序号, 合同号, 合同行号, 入库仓库,入库库位,存货编号, 存货名称, 规格型号, 批次号, 入库数量 ,计量单位 ";
            sql += " FROM Temp_辅料入库 where  1=2";
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            if (this.saveFileDialog1.ShowDialog(this) == DialogResult.OK)
            {
                path = this.saveFileDialog1.FileName;
                if (!File.Exists(path))
                {
                    MiniExcel.SaveAs(path, tb, true, "sheet1", ExcelType.XLSX);
                }
                else
                {
                    MessageBox.Show("文件已经存在,请重试!", "提示", MessageBoxButtons.OK);
                    return;
                }

            }
        }

        private void uiButton导入2_Click(object sender, EventArgs e)
        {
            if (tguid == "")
            {
                tguid = Guid.NewGuid().ToString();
            }
            辅料锌锭导入();
            //try
            //{
            //    string path;

            //    this.openFileDialog1.Filter = "Excel 文件(*.xlsx)|*.xlsx";
            //    if (this.openFileDialog1.ShowDialog(this) == DialogResult.OK)
            //    {
            //        path = this.openFileDialog1.FileName;
            //        DataTable table = (DataTable)MiniExcel.QueryAsDataTable(path, useHeaderRow: true);
            //        if (table.Rows.Count > 0)
            //        {
            //            if (tguid == "")
            //            {
            //                tguid = Guid.NewGuid().ToString();
            //            }
            //            for (int i = 0; i < table.Rows.Count; i++)
            //            {

            //                string sql;
            //                int 序号;
            //                string 合同号;
            //                if (table.Rows[i]["合同行号"].ToString() != "")
            //                {
            //                    合同号 = table.Rows[i]["合同号"].ToString();
            //                }
            //                else
            //                {
            //                    合同号 = this.uiTextBox手填合同号.Text;
            //                }
            //                string 合同行号;
            //                if (table.Rows[i]["合同行号"].ToString() != "")
            //                {
            //                    合同行号 = table.Rows[i]["合同行号"].ToString();
            //                }
            //                else
            //                {
            //                    合同行号 = this.numericUpDown合同行号.Value.ToString();
            //                }


            //                string 存货编号 = table.Rows[i]["存货编号"].ToString();
            //                string 存货名称 = table.Rows[i]["存货名称"].ToString();
            //                string 规格型号 = table.Rows[i]["规格型号"].ToString();
            //                string NC存货编号 = pub.GetInventoryCode(存货名称, 规格型号);
            //                if (NC存货编号 == "")
            //                {
            //                    MessageBox.Show("第" + i.ToString() + "行【" + 存货名称 + "," + 规格型号 + "】没有NC存货编号,请确认数据!", "提示", MessageBoxButtons.OK);
            //                    pub.Delete辅料入库临时数据();
            //                    return;
            //                }
            //                else
            //                {
            //                    if (存货编号 == "")
            //                    {
            //                        存货编号 = NC存货编号;
            //                    }
            //                    else
            //                    {
            //                        if (!pub.IsExistInventory(存货编号))
            //                        {
            //                            MessageBox.Show("第" + i.ToString() + "行【" + 存货编号 + "】的信息不存在,请确认数据!", "提示", MessageBoxButtons.OK);
            //                            pub.Delete辅料入库临时数据();
            //                            return;
            //                        }
            //                        if (存货编号 != NC存货编号)
            //                        {
            //                            MessageBox.Show("第" + i.ToString() + "行【" + 存货编号 + "】和【" + 存货名称 + "," + 规格型号 + "】的信息有冲突,请确认数据!", "提示", MessageBoxButtons.OK);
            //                            pub.Delete辅料入库临时数据();
            //                            return;
            //                        }

            //                    }
            //                }
            //                string 计量单位 = table.Rows[i]["计量单位"].ToString();
            //                string 批次号 = table.Rows[i]["批次号"].ToString();
            //                decimal 入库数量 = pub.Todecimal(table.Rows[i]["入库数量"].ToString());
            //                string 入库仓库编号 = pub.getWareHouse(table.Rows[i]["入库仓库"].ToString());
            //                string 入库仓库 = table.Rows[i]["入库仓库"].ToString();
            //                string 入库库位 = pub.getLocation(table.Rows[i]["入库库位"].ToString());
            //                #region 检查

            //                if (存货编号 == "")
            //                {
            //                    MessageBox.Show("存货编号未选择,请重新选择!", "提示", MessageBoxButtons.OK);
            //                    pub.Delete辅料入库临时数据();
            //                    return;
            //                }
            //                if (批次号 == "")
            //                {
            //                    MessageBox.Show("批次号未填写!", "提示", MessageBoxButtons.OK);
            //                    pub.Delete辅料入库临时数据();
            //                    return;
            //                }
            //                if (入库数量 <= 0)
            //                {
            //                    MessageBox.Show("入库数量有问题!", "提示", MessageBoxButtons.OK);
            //                    pub.Delete辅料入库临时数据();
            //                    return;
            //                }
            //                if (入库仓库编号 == "")
            //                {
            //                    MessageBox.Show("入库仓库有问题!", "提示", MessageBoxButtons.OK);
            //                    pub.Delete辅料入库临时数据();
            //                    return;
            //                }
            //                if (入库库位 == "")
            //                {
            //                    MessageBox.Show("入库库位有问题!", "提示", MessageBoxButtons.OK);
            //                    pub.Delete辅料入库临时数据();
            //                    return;
            //                }
            //                ///检查辅料存货编号和批次是否存在了
            //                if (pub.IsExistBatch(存货编号, 批次号))
            //                {
            //                    MessageBox.Show(存货编号+","+批次号+"已经存在了,不能再次入库!", "提示", MessageBoxButtons.OK);
            //                    pub.Delete辅料入库临时数据();
            //                    return;

            //                }
            //                if (IsExistTempBatch(存货编号, 批次号))
            //                {
            //                    MessageBox.Show("+" + 存货编号 + "," + 批次号 + "这次已经有添加了,不能再次添加!", "提示", MessageBoxButtons.OK);
            //                    pub.Delete辅料入库临时数据();
            //                    return;
            //                }

            //                序号 = i + 1;
            //                if (合同号 == "")
            //                {
            //                    合同行号 = "";
            //                }
            //                else
            //                {
            //                    if (合同行号 == "0")
            //                    {
            //                        合同行号 = "1";
            //                    }
            //                }
            //                #endregion

            //                ///塞入临时表并显示
            //                sql = @"insert into Temp_辅料入库 (guid, 序号, 合同号, 合同行号, 入库仓库,入库仓库编号,入库库位,存货编号, 存货名称, 规格型号, 批次号, 入库数量 ,计量单位, MAC, 时间,T_GOOD_NUM, T_GOOD_PRICE, T_GOOD_MONEY, CID )";
            //                sql += " values ('" + tguid + "', '" + 序号 + "', '" + 合同号 + "', '" + 合同行号 + "','" + 入库仓库 + "','" + 入库仓库编号 + "','" + 入库库位 + "', '" + 存货编号 + "', '" + 存货名称 + "',";
            //                sql += " '" + 规格型号 + "',  '" + 批次号 + "','" + 入库数量 + "','" + 计量单位 + "',";
            //                sql += "'" + pub.getMac() + "',getdate(),'0','0','0','' )";
            //                if (pub.ExecuteSql(pub.ConnectionString, sql, "塞入辅料入库临时表") > 0)
            //                {


            //                }
            //                else
            //                {
            //                    MessageBox.Show("添加失败了请检查网络或者其他原因!", "提示", MessageBoxButtons.OK);

            //                    return;

            //                }
            //            }
            //            ListTemp_辅料入库(tguid, this.uiDataGridView辅料2);
            //        }


            //    }
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show("导入失败了!" + ex.ToString(), "提示", MessageBoxButtons.OK);
            //    pub.Delete辅料入库临时数据();
            //    return;
            //}
        }
        private void 辅料锌锭导入()
        {
            try
            {
                string path;

                this.openFileDialog1.Filter = "Excel 文件(*.xlsx)|*.xlsx";
                if (this.openFileDialog1.ShowDialog(this) == DialogResult.OK)
                {

                    path = this.openFileDialog1.FileName;
                    List<dbmodel.Temp_辅料入库> temp_辅料入库导入 = MiniExcel.Query<dbmodel.Temp_辅料入库>(path).ToList();
                    if (temp_辅料入库导入 == null || temp_辅料入库导入.Count <= 0)
                    {
                        MessageBox.Show("请填写数据");
                        return;
                    }

                    List<dbmodel.Dic_Warehouse> warehouseList = SqlSugarHelp.scope.Queryable<dbmodel.Dic_Warehouse>().ToList();

                    if (temp_辅料入库导入.Find(it => string.IsNullOrWhiteSpace(it.批次号)) != null)
                    {
                        MessageBox.Show("批次号不能为空", "提示", MessageBoxButtons.OK);
                        pub.Delete辅料入库临时数据();
                        return;
                    }
                    if (temp_辅料入库导入.Find(it => string.IsNullOrWhiteSpace(it.入库数量)) != null)
                    {
                        MessageBox.Show("入库数量有问题!", "提示", MessageBoxButtons.OK);
                        pub.Delete辅料入库临时数据();
                        return;
                    }

                    if (temp_辅料入库导入.Find(it => string.IsNullOrWhiteSpace(it.入库仓库)) != null)
                    {
                        MessageBox.Show("入库仓库有问题!", "提示", MessageBoxButtons.OK);
                        pub.Delete辅料入库临时数据();
                        return;
                    }
                    if (temp_辅料入库导入.Find(it => string.IsNullOrWhiteSpace(it.入库库位)) != null)
                    {
                        MessageBox.Show("入库库位不能为空!", "提示", MessageBoxButtons.OK);
                        pub.Delete辅料入库临时数据();
                        return;
                    }

                    //查招采的数据
                    List<dbmodel.VIEW_CIM> VIEW_CIMList = SqlSugarHelp.scope.GetConnection("DASContract")
                                                                            .Queryable<dbmodel.VIEW_CIM>().ToList();
                    foreach (var 锌锭 in temp_辅料入库导入)
                    {
                        锌锭.guid = Guid.Parse(tguid);
                        dbmodel.VIEW_CIM VIEW_CIM = VIEW_CIMList.Find(it => it.T_GOOD_NAME == 锌锭.存货名称
                                                                                         && it.T_GOOD_MODEL == 锌锭.规格型号
                                                                                         && it.T_GOOD_TYPE == "锭类"
                                                                                         && it.T_GOOD_STATUE == "启用");
                        //查仓库
                        dbmodel.Dic_Warehouse warehouse = warehouseList.Find(it => it.WarehouseName == 锌锭.入库仓库);
                        if (warehouse == null)
                        {
                            MessageBox.Show($"锌锭批号{锌锭.批次号},规格型号{锌锭.规格型号}没查到仓库编号，请检查仓库名");
                            return;
                        }
                        else
                        {
                            锌锭.入库仓库编号 = warehouse.WarehouseCode;
                        }
                        if (VIEW_CIM == null)
                        {
                            MessageBox.Show($"锌锭批号{锌锭.批次号},规格型号{锌锭.规格型号}没查到存货编号");
                            return;
                        }
                        else
                        {
                            锌锭.计量单位 = VIEW_CIM.T_GOOD_UNIT;
                            锌锭.存货编号 = VIEW_CIM.T_GOOD_CODE;
                        }
                    }

                    //检查有没有导入过
                    List<string> batchList = temp_辅料入库导入.Select(it => it.批次号).ToList();
                    List<dbmodel.Bus_StockInMaterial> stockInMaterialList = SqlSugarHelp.scope.Queryable<dbmodel.Bus_StockInMaterial>()
                                                                                              .Where(it => batchList.Contains(it.Batch))
                                                                                              .ToList();
                    if (stockInMaterialList != null && stockInMaterialList.Count > 0)
                    {
                        string msg = "";
                        foreach (var stockInMaterial in stockInMaterialList)
                        {
                            msg = msg + stockInMaterial.Batch + ",";
                        }
                        MessageBox.Show(msg + "已经入过库，请换个批号");
                        return;
                    }
                    //上面都没问题，导入
                    SqlSugarHelp.scope.GetConnection("panhuadb").Insertable(temp_辅料入库导入).ExecuteCommand();
                    ListTemp_辅料入库(tguid, this.uiDataGridView辅料2);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("导入失败了!" + ex.ToString(), "提示", MessageBoxButtons.OK);
                pub.Delete辅料入库临时数据();
                return;
            }
        }

        private void uiComboBoxWorkShop2_SelectedIndexChanged(object sender, EventArgs e)
        {
            string sql;
            工厂编号2 = this.uiComboBoxWorkShop2.SelectedValue.ToString();
            sql = @"SELECT   guid, WorkShopCode, WorkShopName, AuditDate, AuditStatus, AuditId, Auditor, Remark, CreateID, Creator, 
                CreateDate, ModifyID, Modifier, ModifyDate   FROM      Dic_WorkShop     where  WorkShopCode='" + 工厂编号2 + "'      ";
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            if (tb.Rows.Count > 0)
            {
                //                sql = @"SELECT    guid, WorkShopGuid, WarehouseCode, WarehouseName, WarehouseDesc, AuditDate, AuditStatus, AuditId, 
                //                Auditor, Remark, CreateID, Creator, CreateDate, ModifyID, Modifier, ModifyDate
                //FROM      Dic_Warehouse where WorkShopGuid ='" + tb.Rows[0]["guid"].ToString() + "'";
                //                DataTable tb2 = pub.GetDataTable(pub.ConnectionString, sql);
                pub.ListWareHouse(this.uiComboBox仓库编号2, tb.Rows[0]["guid"].ToString());
                Do库位刷新2(this.uiComboBox仓库编号2.SelectedValue.ToString());
            }
        }

        private void uiButton新建入库单2_Click(object sender, EventArgs e)
        {

        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
        }

        private void ClearAll2()
        {
            this.uiTextBox批次号2.Text = "";
            this.uiTextBox入库数量2.Text = "";
            this.uiTextBox存货编号2.Text = "";
            this.uiTextBox存货名称2.Text = "";
            this.uiTextBox规格型号2.Text = "";
            this.uiTextBox计量单位2.Text = "";
            this.uiTextBox手填合同号.Text = "";
            this.numericUpDown合同行号.Value   =0;

            this.uiDataGridView辅料2.DataSource = null;
            this.uiDataGridView辅料2.Refresh();
        }

        private void uiButton保存2_Click(object sender, EventArgs e)
        {
            ///将临时表塞入入库单
            ///
            string sql;
            bool t = false;
            ///先验证是否重复了
            sql = "select guid, 序号, 合同号, 合同行号, 存货编号, 存货名称, 规格型号, 计量单位,入库数量, 批次号    FROM Temp_辅料入库 where guid='" + tguid + "'";
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            if (tb.Rows.Count > 0)
            {
                for (int i = 0; i < tb.Rows.Count; i++)
                {
                    if (pub.IsExistBatch(tb.Rows[i]["存货编号"].ToString(), tb.Rows[i]["批次号"].ToString()))
                    {

                        MessageBox.Show("要保存辅料【" + tb.Rows[i]["存货编号"].ToString() + "," + tb.Rows[i]["批次号"].ToString() + "】已经有保存过了!", "提示", MessageBoxButtons.OK);
                        return;
                    }
                }
            }
            string stockInType = "辅料入库";

            string workShopCode = 工厂编号2;
            string docCode = pub.GetDocCode("辅料入库", pub.全局工厂编号);
            if (this.uiTextBox部门2.Text == "")
            {
                MessageBox.Show("部门未选择!", "提示", MessageBoxButtons.OK);
                return;
            }
            string departCode = pub.GetDepartByCode(this.uiTextBox部门2.Text, 工厂编号2).DepartCode;
            string guid = Guid.NewGuid().ToString();
            ///塞入原料主表
            if (docCode == "")
            {
                MessageBox.Show("未获得单号,请联系系统管理员!", "提示", MessageBoxButtons.OK);
                return;
            }

            sql = " insert into Bus_StockInM(guid, StockInType, WorkShopCode, DocCode,  DepartCode, CreateID, Creator, CreateDate,IsSendToNC)";
            sql += " values('" + guid + "','" + stockInType + "','" + pub.全局工厂编号 + "','" + docCode + "', '" + departCode + "','" + pub.sys_user.User_Id + "','" + pub.sys_user.UserTrueName + "', getdate(),'N')";
            sql += @" insert into Bus_StockInMaterial(guid, Bguid, DocCode, RowNumber, WorkShopCode, WarehouseCode,WarehouseName, StorageLocation, T_CONTRACT_CODE, 
                T_ORDER, T_GOOD_UNIT, T_GOOD_NUM, T_GOOD_PRICE, T_GOOD_MONEY, InventoryCode,QuantityMain,QuantityAssist, Size, CID, Batch
            )";
            sql += " select  '" + guid + "',newid(),'" + docCode + "',  序号,'" + workShopCode + "', 入库仓库编号,入库仓库,入库库位,合同号,合同行号,计量单位,0, ";
            sql += " 0, 0,存货编号,入库数量,1,规格型号,'' ,批次号";
            sql += " from  Temp_辅料入库 where guid='" + tguid + "'";
            pub.ExecuteSql(pub.ConnectionString, sql, "保存原料入库单据");
            ///检查是否成功
            sql = "select * from Bus_StockInM where guid='" + guid + "' ";
            DataTable tbr = pub.GetDataTable(pub.ConnectionString, sql);
            if (tbr.Rows.Count > 0)
            {
                sql = "select * from Bus_StockInMaterial where guid='" + guid + "'";
                DataTable tbb = pub.GetDataTable(pub.ConnectionString, sql);
                if (tbb.Rows.Count > 0)
                {
                    MessageBox.Show("保存成功单号【" + docCode + "】!", "提示", MessageBoxButtons.OK);
                    sql = "delete  Temp_辅料入库 where guid='" + tguid + "' or mac ='" + pub.getMac() + "'";
                    pub.ExecuteSql(pub.ConnectionString, sql, "保存成功后删除原料入库临时表");
                    tguid = "";
                    ClearAll2();

                    return;
                }
                else
                {
                    sql = "delete  Bus_StockInM where guid='" + guid + "'";

                    pub.ExecuteSql(pub.ConnectionString, sql, "因为保存失败了，删除辅料入库单");
                    MessageBox.Show("保存失败,代码【err1】!", "提示", MessageBoxButtons.OK);
                    return;
                }
            }
            else
            {
                sql = "delete  Bus_StockInMaterial  where guid='" + guid + "'";

                pub.ExecuteSql(pub.ConnectionString, sql, "因为保存失败了，删除辅料入库单");
                MessageBox.Show("保存失败,代码【err2】!", "提示", MessageBoxButtons.OK);
                return;
            }
        }

        private void uiDataGridView辅料2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                if (this.uiDataGridView辅料2.Columns[e.ColumnIndex].Name == "E删除")
                {
                    string sql;
                    int 序号 = pub.ToInt(this.uiDataGridView辅料2.Rows[e.RowIndex].Cells["E序号"].Value.ToString());
                    sql = " delete Temp_辅料入库 where (guid='" + tguid + "' and 序号='" + 序号 + "') or (mac ='" + pub.getMac() + "' and guid<>'" + tguid + "')";
                    if (pub.ExecuteSql(pub.ConnectionString, sql, "辅料入库临时表删除行") >= 0)
                    {
                        //更新序号
                        sql = "select guid, 序号   FROM      Temp_辅料入库 where guid='" + tguid + "'  order by 序号 ";
                        DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
                        if (tb != null)
                        {
                            for (int i = 0; i < tb.Rows.Count; i++)
                            {
                                sql = "update Temp_辅料入库 set 序号='" + (i + 1) + "'   where guid='" + tguid + "' and 序号='" + tb.Rows[i]["序号"].ToString() + "'";
                                pub.ExecuteSql(pub.ConnectionString, sql, "更新辅料入库临时表的行号");
                            }
                        }
                        ListTemp_辅料入库(tguid, this.uiDataGridView辅料2);
                    }
                    else
                    {
                        MessageBox.Show("删除出错了,请检查");
                        return;
                    }

                }
            }
        }

        private void uiButton添加2_Click(object sender, EventArgs e)
        {
            if (tguid == "")
            {
                tguid = Guid.NewGuid().ToString();
            }
            string sql;
            int 序号;
            if (string.IsNullOrEmpty(this.uiComboBox仓库编号2.SelectedValue?.ToString()))
            {
                MessageBox.Show("请选择仓库");
                return;
            }

            string 合同号 = this.uiTextBox手填合同号.Text.Trim().Replace("--", "");
            string 合同行号 = this.numericUpDown合同行号.Value.ToString();
            string 存货编号 = this.uiTextBox存货编号2.Text.Trim().Replace("--", "");
            string 存货名称 = this.uiTextBox存货名称2.Text.Trim().Replace("--", "");
            string 规格型号 = this.uiTextBox规格型号2.Text.Trim().Replace("--", "");
            string 计量单位 = this.uiTextBox计量单位2.Text.Trim().Replace("--", "");
            string 批次号 = this.uiTextBox批次号2.Text.Trim().Replace("--", "");
            decimal 入库数量 = pub.Todecimal(this.uiTextBox入库数量2.Text.Trim().Replace("--", ""));
            string 入库仓库 = this.uiComboBox仓库编号2.SelectedValue.ToString();
            string 入库库位 = this.uiComboBox入库库位2.SelectedValue?.ToString();
            #region 检查
            //if (合同号 == "")
            //{
            //    MessageBox.Show("合同号未选择,请重新选择!", "提示", MessageBoxButtons.OK);
            //    return;
            //}
            //if (合同行号 == "")
            //{
            //    MessageBox.Show("合同行未选择,请重新选择!", "提示", MessageBoxButtons.OK);
            //    return;
            //}
            if (存货编号 == "")
            {
                MessageBox.Show("存货编号未选择,请重新选择!", "提示", MessageBoxButtons.OK);
                return;
            }
            if (入库仓库 == "")
            {
                MessageBox.Show("入库仓库有问题!", "提示", MessageBoxButtons.OK);
                return;
            }
            if (入库库位 == "")
            {
                MessageBox.Show("入库库位有问题!", "提示", MessageBoxButtons.OK);
                return;
            }
            ///检查原料卷号是否存在了
            if (pub.IsExistBatch(存货编号, 批次号))
            {
                MessageBox.Show("批次号已经存在不能添加了!", "提示", MessageBoxButtons.OK);
                return;

            }
            if (pub.IsExistTempBatch(存货编号, 批次号))
            {
                MessageBox.Show("批次号已经存在不能添加了!", "提示", MessageBoxButtons.OK);
                return;

            }
            if (入库数量 == 0)
            {
                MessageBox.Show("数量不能为0!", "提示", MessageBoxButtons.OK);
                return;
            }
            序号 = this.uiDataGridView辅料2.Rows.Count + 1;
            if (合同号 == "")
            {
                合同行号 = "0";
            }
            else
            {
                if (合同行号 == "0")
                {
                    合同行号 = "1";
                }
            }
            #endregion
            ///塞入临时表并显示
            sql = @"insert into Temp_辅料入库 (guid, 序号, 合同号, 合同行号, 入库仓库,入库库位,存货编号, 存货名称, 规格型号, 批次号, 入库数量 ,计量单位, MAC, 时间, 
                T_GOOD_NUM, T_GOOD_PRICE, T_GOOD_MONEY, CID )";
            sql += " values ('" + tguid + "', '" + 序号 + "', '" + 合同号 + "', '" + 合同行号 + "','" + 入库仓库 + "','" + 入库库位 + "', '" + 存货编号 + "', '" + 存货名称 + "', '" + 规格型号 + "',  '" + 批次号 + "','" + 入库数量 + "','" + 计量单位 + "',";
            sql += "'" + pub.getMac() + "',getdate(),'" + t_view_con.T_GOOD_NUM + "','" + t_view_con.T_GOOD_PRICE + "','" + t_view_con.T_GOOD_MONEY + "','" + t_view_con.CID + "' )";
            if (pub.ExecuteSql(pub.ConnectionString, sql, "塞入辅料入库临时表") > 0)
            {
                //更新序号
                sql = "select guid, 序号   FROM      Temp_辅料入库 where guid='" + tguid + "'  order by 序号 ";
                DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
                if (tb != null)
                {
                    for (int i = 0; i < tb.Rows.Count; i++)
                    {
                        sql = "update Temp_辅料入库 set 序号='" + (i + 1) + "'   where guid='" + tguid + "' and 序号='" + tb.Rows[i]["序号"].ToString() + "'";
                        pub.ExecuteSql(pub.ConnectionString, sql, "更新辅料入库临时表");
                    }
                }
                ListTemp_辅料入库(tguid, this.uiDataGridView辅料2);
            }
            else
            {
                MessageBox.Show("添加失败了请检查网络或者其他原因!", "提示", MessageBoxButtons.OK);
                return;

            }
        }

        private void splitContainer5_SplitterMoved(object sender, SplitterEventArgs e)
        {

        }

        private void updateInvCode()
        {
            string sql = @"select guid, Bguid, DocCode, RowNumber, WorkShopCode, WarehouseCode,WarehouseName, StorageLocation, T_CONTRACT_CODE, 
                T_ORDER, T_GOOD_UNIT, T_GOOD_NUM, T_GOOD_PRICE, T_GOOD_MONEY, InventoryCode,QuantityMain,QuantityAssist, Size, CID, Batch from Bus_StockInMaterial 
                where 1=1 and IsSendToNC <> 'Y' ";
            DataTable dt = pub.GetDataTable(pub.ConnectionString,sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    string bguid = dt.Rows[i]["Bguid"].ToString();
                    string Size = dt.Rows[i]["Size"].ToString();

                    string NC存货编号 = pub.GetInventoryCode("锌锭", Size);
                    //更新
                    string sqlupdate = "update Bus_StockInMaterial set InventoryCode = '"+ NC存货编号 + "' where Bguid = '"+ bguid + "'";
                    int r = pub.ExecuteSql(pub.ConnectionString,sqlupdate,"");
                }
            }
        }
    }
}
